package org.gap.service;

import java.util.List;
import java.util.Set;

import org.gap.dao.NafDAO;

import org.gap.domain.Naf;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Naf entities
 * 
 */

@Service("NafService")
@Transactional
public class NafServiceImpl implements NafService {

	/**
	 * DAO injected by Spring that manages Naf entities
	 * 
	 */
	@Autowired
	private NafDAO nafDAO;

	/**
	 * Instantiates a new NafServiceImpl.
	 *
	 */
	public NafServiceImpl() {
	}

	/**
	 */
	@Transactional
	public Naf findNafByPrimaryKey(String nafNaf) {
		return nafDAO.findNafByPrimaryKey(nafNaf);
	}

	/**
	 * Save an existing Naf entity
	 * 
	 */
	@Transactional
	public void saveNaf(Naf naf) {
		Naf existingNaf = nafDAO.findNafByPrimaryKey(naf.getNafNaf());

		if (existingNaf != null) {
			if (existingNaf != naf) {
				existingNaf.setNafNaf(naf.getNafNaf());
				existingNaf.setNafLibelle(naf.getNafLibelle());
			}
			naf = nafDAO.store(existingNaf);
		} else {
			naf = nafDAO.store(naf);
		}
		nafDAO.flush();
	}

	/**
	 * Return all Naf entity
	 * 
	 */
	@Transactional
	public List<Naf> findAllNafs(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Naf>(nafDAO.findAllNafs(startResult, maxRows));
	}

	/**
	 * Load an existing Naf entity
	 * 
	 */
	@Transactional
	public Set<Naf> loadNafs() {
		return nafDAO.findAllNafs();
	}

	/**
	 * Return a count of all Naf entity
	 * 
	 */
	@Transactional
	public Integer countNafs() {
		return ((Long) nafDAO.createQuerySingleResult("select count(o) from Naf o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing Naf entity
	 * 
	 */
	@Transactional
	public void deleteNaf(Naf naf) {
		nafDAO.remove(naf);
		nafDAO.flush();
	}
}
